<!-- 工期风险与影响事件 -->
<template>
  <base-breadcrumb :title="$route.meta.title">
    <a-spin :spinning="loading">
      <a-card>
        <title-name title="基本信息" />
        <custom-form
          ref="customForm"
          :is-validate="true"
          :edit-model="''"
          :rules="rules"
          :form="form"
          :is-emit="false"
          :form-list="formOption"
          @selectOrg="selectOrg"
          @getProjectInfo="getAuditProjectList"
        >
        </custom-form>
      </a-card>
      <div class="tool-btns page-btn-right-top">
        <a-button v-if="editable" class="ml20" :loading="submitLoading" type="primary" @click="handleSubmit(1)"> 提交 </a-button>
        <a-button class="ml20" @click="$close"> 关闭 </a-button>
      </div>
    </a-spin>
  </base-breadcrumb>
</template>

<script>
import { searchOption, columns, mockdata, eventType, childEventType } from './model.js'
import ASelectOrgPeople from "@/components/Select/ASelectOrgPeople.vue"
import SelectSubCompany from "@/components/Select/SelectSubCompany"
import SelectProject from "@/components/Select/SelectProject"
import UploadList from '@/components/UploadList'
import BSelect from "@/components/BComponents/BSelect.vue"
import moment from "moment"
import { queryTreeLabelList, getProjectRiskEventById, insertSelective, updateByPrimaryKeySelective } from "@/api/project/limitTimeRisk"
export default {
  name: 'limitTimeRisk',
  components: {
    SelectSubCompany,
    ASelectOrgPeople,
    SelectProject,
    UploadList,
    BSelect,
  },
  data() {
    return {
      loading: false,
      defLoading: false,
      submitLoading: false,
      form: {
        eventType: undefined,
        startTime: null,
        endTime: null,
        fileInfoList: []
      },
    }
  },
  computed: {
    pageType() {
      return this.$route.query.type
    },
    editable() {
      return this.pageType == 'add' || this.pageType == 'edit'
    },
    userInfo() {
      return this.$userInfo()
    },
    // 表单配置
    formOption() {
      const list = [
        {
          label: "所属单位",
          keyName: "projectId",
          disabled: !this.editable,
          type: 'orgTree',
          span: 16,
          isNeedAuth: true,
          checkStrictly: true,
          isAutoSelectFirst: true,
          rules: [{ required: true, message: "请选择", trigger: "change" }]
        },
        // {
        //   label: "分公司",
        //   keyName: "branchId",
        //   disabled: !this.editable,
        //   type: SelectSubCompany,
        //   attrs: {
        //     //表单项关联
        //     autoSelected: this.pageType == 'add'
        //   },
        //   rules: [{ required: true, message: "请选择", trigger: "change" }],
        //   selectChange: (e) => {
        //     this.form.branchName = e?.orgName || ''
        //   }
        // },
        // {
        //   label: "项目名称",
        //   keyName: "projectId",
        //   type: SelectProject,
        //   attrs: {
        //     autoSelected: true,
        //     //表单项关联
        //     keyValue: "branchId",
        //     isEdit: true,
        //     form: this.form
        //   },
        //   disabled: !this.editable,
        //   rules: [{ required: true, message: "请选择", trigger: "change" }],
        //   selectChange: (e) => {
        //     if (!e) {
        //       this.form.projectCode = ''
        //       this.form.projectName = ''
        //       return
        //     }
        //     this.form.projectCode = e.$origin.projectNum
        //     this.form.projectName = e.name
        //   }
        // },
        {
          label: "项目编码",
          keyName: "projectCode",
          type: "BaseInput",
          disabled: true,
        },
        {
          label: "事件类型",
          keyName: "eventType",
          disabled: !this.editable,
          type: BSelect,
          rules: [{ required: true, message: "请选择", trigger: "change" }],
          selectChange: (e) => {
            this.form.eventTypeName = e?.name || ''
          },
          attrs: {
            request: this.getEventType,
            form: this.form
          }
        },
        {
          label: "子类型",
          keyName: "childType",
          disabled: !this.editable,
          type: BSelect,
          rules: [{ required: true, message: "请选择", trigger: "change" }],
          selectChange: (e) => {
            this.form.childTypeName = e?.name || ''
          },
          attrs: {
            init: false,
            isEdit: true,//this.pageType == 'add',
            keyValue: "eventType",
            request: this.getChildEventType,
            form: this.form
          }
        },
        {
          label: "事件描述",
          keyName: "eventDescription",
          disabled: !this.editable,
          type: "BaseTextarea",
          rules: [{ required: true, message: "请输入", trigger: "blur" }],
          span: 24
        },
        {
          label: "受影响单位工程",
          keyName: "affectedProject",
          disabled: !this.editable,
          type: "BaseInput",
          rules: [{ required: true, message: "请输入", trigger: "blur" }],
          attrs: {
            maxLength: 100
          },
          span: 24
        },
        {
          label: "受影响施工内容（分部分项）",
          keyName: "affectedContents",
          disabled: !this.editable,
          type: "BaseTextarea",
          rules: [{ required: true, message: "请输入", trigger: "blur" }],
          span: 24
        },
        {
          label: "开始时间",
          keyName: "startTime",
          type: "a-date-picker",
          attrs: {
            format:"YYYY-MM-DD",
            valueFormat: "YYYY-MM-DD HH:mm:ss",
            disabledDate: this.disabledStart
          },
          rules: [{ required: true, message: "请选择", trigger: "change" }],
          disabled: !this.editable,
        },
        {
          label: "结束时间",
          keyName: "endTime",
          type: "a-date-picker",
          attrs: {
            format:"YYYY-MM-DD",
            valueFormat: "YYYY-MM-DD HH:mm:ss",
            disabledDate: this.disabledEnd
          },
          rules: [{ required: true, message: "请选择", trigger: "change" }],
          disabled: !this.editable,
        },
        {
          label: "延期天数",
          keyName: "delayDay",
          disabled: !this.editable,
          type: "BaseInputNumber",
          attrs: {
            min: 0,
            isInt: true
          },
          rules: [{ required: true, message: "请输入", trigger: "blur" }],
        },
        {
          label: "对竣工造成延期天数",
          keyName: "completionDelayDay",
          disabled: !this.editable,
          type: "BaseInputNumber",
          attrs: {
            min: 0,
            isInt: true
          },
          rules: [{ required: true, message: "请输入", trigger: "blur" }],
        },
        {
          label: "事件跟进责任人",
          keyName: "responsibleId",
          disabled: !this.editable,
          type: ASelectOrgPeople,
          attrs: {
            optionLabelProp: 'label',
            customName: (e) => {
              return e.name + e.phone
            }
          },
          rules: [{ required: true, message: "请选择", trigger: "change" }],
          onSelectChange: (e) => {
            this.form.responsibleName = e?.name || ''
          }
        },
        {
          label: "与相关方确认情况",
          keyName: "confirmSituation",
          disabled: !this.editable,
          type: "BaseInput",
          rules: [{ required: true, message: "请输入", trigger: "blur" }],
        },
        {
          label: "创建人",
          keyName: "creatorName",
          disabled: true,
          type: "BaseInput",
        },
        {
          label: "创建时间",
          keyName: "createTime",
          disabled: true,
          type: "BaseInput",
        },
        {
          keyName: 'fileInfoList',
          type: UploadList,
          span: 24,
          disabled: (!this.editable),
          label: '附件',
          attrs: {
            businessCode: 'PM44',
            uploadArrys: this.form.fileInfoList,
            isShow: this.editable,
            needChineseName: true
          },
          fileArrysData: (e) => {
            this.$refs.customForm.validateField('fileInfoList')
          }
        },
      ]
      return list
    },
    // 校验
    rules() {
      const obj = {}
      this.formOption.map((item) => {
        if (item.rules) {
          obj[item.keyName] = item.rules
        }
      })
      return obj
    },
  },

  watch: {
  },
  created() {
    this.initPage()
  },
  mounted() {
  },
  methods: {
    selectOrg(val) {
      this.form.branchName = val[val.length - 2].orgName
      this.form.projectId = val[val.length - 1].projectId
      this.form.branchId =val[val.length - 2].orgId
      this.$refs.customForm.clearValidate(['projectId'])

    },
    getAuditProjectList(val) {
          this.form.projectCode = val.projectNum
          this.form.projectName = val.projectName
          this.$forceUpdate()
    },
    initPage() {
      if (this.pageType == 'add') {
        this.form.creatorName = this.userInfo.name
        this.form.createTime = moment().format('YYYY-MM-DD')
      } else {
        this.loading = true
        getProjectRiskEventById({id: this.$route.query.id}).then((res) => {
          this.loading = false
          if (res.code == 0) {
            this.form = res.data
          }
        }).catch(() => {
          this.loading = false
        })
      }
    },
    getEventType() {
      return queryTreeLabelList({type: 10}).then((res) => {
        if (res.code == 0) {
          return { code: 200, data: res.data }
        }
        return { code: 200, data: [] }
      })
    },
    getChildEventType(id) {
      return queryTreeLabelList({parentId: id}).then((res) => {
        if (res.code == 0) {
          return { code: 200, data: res.data }
        }
        return { code: 200, data: [] }
      })
    },
    handleSubmit() {
      let that = this

      this.$refs.customForm.validate((valid) => {
        if (valid) {
          this.$confirm({
            title: '温馨提示',
            content: '请确认是否要提交，提交后将给项目相关领导班子推送提示信息？',
            okText: '确认',
            okType: 'danger',
            cancelText: '取消',
            onOk() {
              return that.submitReuqest()
            },
          })
        } else {
          this.$message.error('请填写完整信息!')
        }
      })
    },
    submitReuqest() {
      let api = this.pageType == 'add' ? insertSelective : updateByPrimaryKeySelective
      let params = this.$clone(this.form)
      this.submitLoading = true
      return api(params).then((res) => {
        this.submitLoading = false
          if (res.code == 0) {
            this.$message.success('提交成功！')
            this.$close()
          }
      }).catch((err) => {
        this.submitLoading = false
      })
    },
    disabledStart(startValue) {
      const endValue = this.form.endTime;
      if (!startValue || !endValue) {
        return false;
      }
      return startValue.valueOf() > moment(endValue).valueOf();
    },
    disabledEnd(endValue) {
      const startValue = this.form.startTime;
      if (!endValue || !startValue) {
        return false;
      }
      return moment(startValue).valueOf() >= endValue.valueOf();
    },
  }
}
</script>
<style lang='less' scoped>
</style>
